package com.ruoyi.service.service.impl;

import java.util.List;

import com.ruoyi.archive.domain.ArchiveRoom;
import com.ruoyi.archive.mapper.ArchiveRoomMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.service.domain.ServiceCharge;
import com.ruoyi.service.mapper.ServiceChargeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.validation.Validator;
import com.ruoyi.service.mapper.ServiceFitmentMapper;
import com.ruoyi.service.domain.ServiceFitment;
import com.ruoyi.service.service.IServiceFitmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 装修信息Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-21
 */
@Service
public class ServiceFitmentServiceImpl implements IServiceFitmentService 
{
    private static final Logger log = LoggerFactory.getLogger(ServiceFitmentServiceImpl.class);

    @Autowired
    private ServiceFitmentMapper serviceFitmentMapper;
    @Autowired
    private ServiceChargeMapper serviceChargeMapper;
    @Autowired
    private ArchiveRoomMapper archiveRoomMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询装修信息
     * 
     * @param id 装修信息主键
     * @return 装修信息
     */
    @Override
    public ServiceFitment selectServiceFitmentById(Long id)
    {
        return serviceFitmentMapper.selectServiceFitmentById(id);
    }

    /**
     * 查询装修信息列表
     * 
     * @param serviceFitment 装修信息
     * @return 装修信息
     */
    @Override
    public List<ServiceFitment> selectServiceFitmentList(ServiceFitment serviceFitment)
    {
        return serviceFitmentMapper.selectServiceFitmentList(serviceFitment);
    }

    /**
     * 新增装修信息
     * 
     * @param serviceFitment 装修信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertServiceFitment(ServiceFitment serviceFitment)
    {
        if(serviceFitment.getDeptId() == null){
            ArchiveRoom archiveRoom = archiveRoomMapper.selectArchiveRoomById(serviceFitment.getRoomId());
            serviceFitment.setDeptId(archiveRoom.getDeptId());
        }
        serviceFitment.setCreateTime(DateUtils.getNowDate());

        //先生成收费单
        ServiceCharge serviceCharge = new ServiceCharge();
        BeanUtils.copyProperties(serviceFitment, serviceCharge);
        serviceCharge.setFeeType(ServiceCharge.TYPE_DEPOSIT);
        serviceCharge.setStatus(serviceFitment.getChargeStatus());
        serviceCharge.setFeeShould(serviceFitment.getFeeDeposit());
        serviceCharge.setFeeReal(serviceFitment.getFeeDeposit());
        serviceCharge.genNum();
        serviceChargeMapper.insertServiceCharge(serviceCharge);

        serviceFitment.setChargeId(serviceCharge.getId());

        return serviceFitmentMapper.insertServiceFitment(serviceFitment);
    }

    /**
     * 修改装修信息
     * 
     * @param serviceFitment 装修信息
     * @return 结果
     */
    @Override
    public int updateServiceFitment(ServiceFitment serviceFitment)
    {
        serviceFitment.setUpdateTime(DateUtils.getNowDate());
        return serviceFitmentMapper.updateServiceFitment(serviceFitment);
    }

    /**
     * 批量删除装修信息
     * 
     * @param ids 需要删除的装修信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceFitmentByIds(Long[] ids)
    {
        return serviceFitmentMapper.deleteServiceFitmentByIds(ids);
    }

    /**
     * 删除装修信息信息
     * 
     * @param id 装修信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceFitmentById(Long id)
    {
        return serviceFitmentMapper.deleteServiceFitmentById(id);
    }
}
